<?php

namespace app\finance\controller;

use app\common\controller\Adminbase;
use app\common\controller\ListBuilder;
use app\Constant\OrderConstant;
use app\model\AdminAgent;
use app\model\Order as OrderModel;

class Order extends Adminbase
{
	public function index()
	{
		$model = 'Order';

		if ($this->request->isAjax()) {
			$map = $this->buildParamsMap();

			// 对于筛选条件自定义处理
			foreach ($map as &$item) {
				if ($item[0] == 'is_user') {
					if ($item[2] == 0) {
						$item = ['user_id', 'eq', 0];
					} else {
						$item = ['user_id', 'neq', 0];
					}
				}
			}

			$page     = input('page') ? input('page') : 1;
			$pageSize = input('limit') ? input('limit') : 20;

			$list   = OrderModel::where($map)
				->field('*')
				->withAttr('total', function ($value) {
					return $value > 0 ? number_format($value / 100, 2) : 0;
				})
				->withAttr('actual_commission', function ($value) {
					return $value > 0 ? number_format($value / 100, 2) : 0;
				})
				->withAttr('commission', function ($value) {
					return $value > 0 ? number_format($value / 100, 2) : 0;
				})
				->withAttr('share_commission', function ($value) {
					return $value > 0 ? number_format($value / 100, 2) : 0;
				})
				->withAttr('subsidy_commission', function ($value) {
					return $value > 0 ? number_format($value / 100, 2) : 0;
				})
				->withAttr('one_commission', function ($value) {
					return $value > 0 ? number_format($value / 100, 2) : 0;
				})
				->withAttr('agent_commission', function ($value) {
					return $value > 0 ? number_format($value / 100, 2) : 0;
				})
//                ->withAttr('agent_subsidy_commission', function ($value) {
//                    return $value > 0 ? number_format($value / 100, 2) : 0;
//                })
				->withAttr('profit', function ($value) {
					return $value != 0 ? number_format($value / 100, 2) : 0;
				})
				->withAttr('is_free', function ($value) {
//                    $text = [1 => "<span class='layui-badge layui-bg-green'>是</span>", 0 => "<span class='layui-badge'>否</span>"];
					$text = [1 => "是", 0 => "否"];
					return $text[$value];
				})
				->withAttr('status', function ($value) {
					return OrderConstant::ORDER_STATUS[$value];
				})
				->withAttr('brand_id', function ($value) {
					return OrderConstant::BRAND_LIST[$value];
				})
				->withAttr('commission_status', function ($value) {
					$text = [20 => "<span class='layui-badge layui-bg-blue'>未结算</span>", 100 => "<span class='layui-badge layui-bg-green'>已结算</span>", 200 => "<span class='layui-badge'>取消结算</span>"];
					return $text[$value];
				})
				->page($page, $pageSize)
				->order('id', 'desc')
				->select();
			$total  = OrderModel::where($map)->count();
			$result = ["code" => 0, "count" => $total, "data" => $list];
			return json($result);
		}

		$status  = OrderConstant::ORDER_STATUS;
		$tabList = [
			['key' => '', 'value' => '全部'],
		];
		foreach ($status as $key => $value) {
			$tabList[] = [
				'key'   => $key,
				'value' => $value,
			];
		}

		//生成列表数据
		$builder = new ListBuilder();
		$builder
			->setDataUrl()
			->setConfirmBtn([
				['title' => '删除无效订单', 'url' => url('delete'), 'class' => 'layui-btn-danger', 'msg' => '是否确认删除无效订单？删除后将无法恢复，请谨慎操作！'],
			])
			->setAddBtn(false)
			->setDelAllBtn(false)
			->setDelBtn(false)
			->setTabNav($tabList)
			->setTabName('status')
			->setLimit()
			->addSearchItem('order_id', 'text', '订单号')
			->addSearchItem('commission_status', 'select', '佣金状态', OrderConstant::COMMISSION_STATUS)
			->addSearchItem('is_user', 'select', '是否有推客', OrderConstant::IS_USER)
			->setModel($model)
			->addTableCheckBox()
			->addTableColumn('id', 'ID', null, null, true)
			->addTableColumn('brand_id', '品牌', 180)
			->addTableColumn('order_id', '订单号', 180)
			->addTableColumn('user_id', '推客ID')
			->addTableColumn('product_id', '商品ID', 150)
			->addTableColumn('title', '商品名称', 300)
			->addTableColumn('commission_status', '佣金状态', 100)
			->addTableColumn('status', '订单状态', 100)
			->addTableColumn('is_free', '是否是0元购', 110)
			->addTableColumn('total', '订单金额', 150)
			->addTableColumn('actual_commission', '总佣金', 150)
			->addTableColumn('agent_commission', '团长佣金', 150)
			->addTableColumn('commission', '直推或自购佣金', 150)
			->addTableColumn('one_commission', '推客上级分销佣金', 150)
//            ->addTableColumn('share_commission', '推客佣金佣金', 150)
//            ->addTableColumn('subsidy_commission', '商品补贴佣金', 150)
//            ->addTableColumn('agent_subsidy_commission', '团长补贴佣金', 150)
			->addTableColumn('profit', '平台毛利', 150)
			->addTableColumn('create_time', '创建时间', 180)
			->addTableOperation()
			->setListLayerBtn([
				['title' => '佣金明细', 'url' => url('detail'), 'area' => "['60vw', '80vh']"],
			])
			->returnForm();

		$this->assign('text', "佣金介绍：<br />推客佣金 = 直推或自购佣金 + 商品补贴佣金；<br />一级佣金 = 总佣金 * 下级分销比例；<br />平台毛利 = 总佣金 - 推客佣金 - 团长佣金<br />团长佣金 = 总佣金 * 团长佣金比例 + 团长补贴佣金；<br />如果订单属于免单，则商品补贴佣金=商品售价-总佣金<br />");
		$this->assign('module', 'finance');
		return $this->fetch('admin@common/lists');
	}

	public function detail()
	{
		$id   = $this->request->param('id');
		$info = OrderModel::where('id', $id)
			->field('*')
			->with(['user'])
			->withAttr('total', function ($value) {
				return $value > 0 ? number_format($value / 100, 2) : 0;
			})
			->withAttr('actual_commission', function ($value) {
				return $value > 0 ? number_format($value / 100, 2) : 0;
			})
			->withAttr('commission', function ($value) {
				return $value > 0 ? number_format($value / 100, 2) : 0;
			})
			->withAttr('share_commission', function ($value) {
				return $value > 0 ? number_format($value / 100, 2) : 0;
			})
			->withAttr('subsidy_commission', function ($value) {
				return $value > 0 ? number_format($value / 100, 2) : 0;
			})
			->withAttr('one_commission', function ($value) {
				return $value > 0 ? number_format($value / 100, 2) : 0;
			})
			->withAttr('agent_commission', function ($value) {
				return $value > 0 ? number_format($value / 100, 2) : 0;
			})
			->withAttr('agent_push_commission', function ($value) {
				return $value > 0 ? number_format($value / 100, 2) : 0;
			})
			->withAttr('agent_shop_commission', function ($value) {
				return $value > 0 ? number_format($value / 100, 2) : 0;
			})
			->withAttr('profit', function ($value) {
				return $value != 0 ? number_format($value / 100, 2) : 0;
			})
			->withAttr('status', function ($value) {
				return OrderConstant::ORDER_STATUS[$value];
			})
			->withAttr('commission_status', function ($value) {
				return OrderConstant::COMMISSION_STATUS[$value];
			})
			->find();

		$agent = '';
		if ($info['agent_shop_agent'] > 0 && $info['agent_id'] != $info['agent_shop_agent']) {
			$agent = AdminAgent::where('id', $info['agent_shop_agent'])->value('name');
		}
		//合作商总佣金
		$agent_actual_commission         = $info['agent_commission'] - $info['agent_push_commission'] - $info['agent_shop_commission'];
		$info['agent_actual_commission'] = $agent_actual_commission;
		if ($info['agent_id'] != $info['agent_shop_agent']) {
			$info['total_agent_commission'] = $agent_actual_commission + floor($info['agent_push_commission']);
		} else {
			$info['total_agent_commission'] = $info['agent_commission'];
		}
		$this->assign([
			'info'  => $info,
			'agent' => $agent,
		]);
		return $this->fetch('order/detail');
	}

	public function delete()
	{
		$res = OrderModel::where('status', 'in', [200, 250])->delete();
		if ($res !== false) {
			return json(['code' => 0, 'msg' => '删除成功']);
		}
		return json(['code' => -1, 'msg' => '删除失败']);
	}


}
